package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service(timeout = 500000)
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 需求描述：往quanzi_comment评论表插入数据并更新动态表中点赞数量，且返回总点赞数
    @Override
    public Long save(Comment comment) {
        //1. 保存评论
        mongoTemplate.save(comment);

        //2. 更新动态表中点赞数量/喜欢数量 加1
        //2.1 更新条件，根据动态id更新
        Query query = new Query(
            Criteria.where("id").is(comment.getPublishId())
        );
        //2.2 更新的数据
        Update update = new Update();
        //update.inc("likeCount",1);
        update.inc(comment.getCol(),1);
        mongoTemplate.updateFirst(query,update, Publish.class);

        //3. 返回总点赞数/喜欢数: 查询评论表，查询条件：动态id、评论类型
        Query commentQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
        );
        Long count = mongoTemplate.count(commentQuery, Comment.class);
        return count;
    }

    @Override
    public Long delete(Comment comment) {
        //需求分析：删除评论表中点赞数据、修改动态表点赞总数减1、返回点赞数量

        //1. 删除动态
        //1.1 构造条件：用户id、动态id、评论类型
        Query query = Query.query(
                Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
        );
        //1.2 删除
        mongoTemplate.remove(query,Comment.class);

        //2. 修改动态表，点赞总数减去1
        Query publishQuery = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1);
        mongoTemplate.updateFirst(publishQuery,update, Publish.class);

        //3. 统计点赞/喜欢总数
        Query commentQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
        );
        Long count = mongoTemplate.count(commentQuery, Comment.class);
        return count;
    }

    @Override
    public PageResult findByPage(String movementId, Integer page, Integer pagesize) {
        // 需求描述：分页查询评论，查询条件：动态id、评论类型
        // 构造分页条件
        Query query = Query.query(
                Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(2)
        );
        query.skip((page-1)*pagesize).limit(pagesize);
        query.with(Sort.by(Sort.Order.desc("created")));
        // 查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        Long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,count.intValue(),commentList);
    }


    @Override
    public PageResult findCommentsByUserId(
            Long userId, Integer commentType, Integer page, Integer pagesize) {
        // 注意：这里的条件userId是发布者的用户id，而Comment表中只存储了评论人id
        // 此时，如果根据发布者的id查询评论就无法实现了。
        // 所以：quanzi_comment评论表中要添加一个字段publishUserId。实体类也要加一个对应属性
        // mongo中表添加字段：
        // db.quanzi_comment.update({},{$set:{publishUserId:20}},{multi:true})
        Query query = new Query(
                Criteria.where("commentType").is(commentType)
                        .and("publishUserId").is(userId)
        );
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize, (int) count,commentList);
    }



}















